package priv.demo3.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.payment.lc.pcs.common.enums.DisplayType;
import com.payment.lc.pcs.common.enums.MiProductCategory;

import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Scanner;

public class Main4 {
    public static void main(String[] args) throws Exception {
        StringBuilder s = new StringBuilder();

        Scanner scanner = new Scanner(new FileInputStream(new File("D:/tmp/products")));
        while (scanner.hasNextLine()) {
            s.append(scanner.nextLine());
        }
        scanner.close();

        Map map = JSON.parseObject(s.toString(), Map.class);
        JSONArray records = (JSONArray) map.get("records");
        System.out.println(records.size());

        Collections.sort(records, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                Object miCategory = ((Map) o1).get("miCategory");
                MiProductCategory c1 = MiProductCategory.valueOf((String) miCategory);
                Object miCategory2 = ((Map) o2).get("miCategory");
                MiProductCategory c2 = MiProductCategory.valueOf((String) miCategory2);
                int i = c1.ordinal() - c2.ordinal();
                if (i != 0) {
                    return i;
                }
                String providerName1 =(String) ((Map) o1).get("providerName");
                String providerName2 = (String) ((Map) o2).get("providerName");
                i = providerName1.compareTo(providerName2);
                if (i != 0) {
                    return i;
                }

                String d1 =(String) ((Map) o1).get("displayType");
                String d2 = (String) ((Map) o2).get("displayType");
                return d1.compareTo(d2);
            }

        });

        for (int i = 0; i < records.size(); i++) {
            JSONObject o = (JSONObject) records.get(i);
            Object productionCode = o.get("productionCode");
            Object name = o.get("name");
            Object miCategory = o.get("miCategory");
            Object providerName = o.get("providerName");
            Object displayType = o.get("displayType");

            StringBuilder p = new StringBuilder();
            p.append(productionCode);
            p.append("  ");
            p.append(name);
            p.append("  ");
            p.append(MiProductCategory.valueOf((String) miCategory).getText());
            p.append("  ");
            p.append(providerName);
            p.append("  ");
            p.append(DisplayType.valueOf((String) displayType).getText());

            System.out.println(p);
        }

    }
}